package com.osce.go_score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.go_score.GoScoreExamPlace;
import com.csce.domain.entity.go_score.GoScoreExamStudent;
import com.doudou.core.container.ListUtil;
import com.doudou.core.lang.ObjUtil;
import com.osce.go_score.mapper.GoScoreExamPlaceMapper;
import com.osce.interfaces.go_score.IGoScoreExamPlaceService;
import com.osce.interfaces.go_score.IGoScoreExamStudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 妇产科考试-考场Service业务层处理
 *
 * @author zzx
 * @since 2024-05-17
 */
@Service
public class GoScoreExamPlaceServiceImpl extends ServiceImpl<GoScoreExamPlaceMapper, GoScoreExamPlace> implements IGoScoreExamPlaceService {

    @Resource private IGoScoreExamStudentService goScoreExamStudentService;

    /**
     * 查询妇产科考试-考场
     *
     * @param id 妇产科考试-考场主键
     * @return 妇产科考试-考场
     */
    @Override
    public GoScoreExamPlace selectGoScoreExamPlaceById(Long id) {
        return baseMapper.selectGoScoreExamPlaceById(id);
    }

    /**
     * 查询妇产科考试-考场列表
     *
     * @param goScoreExamPlace 妇产科考试-考场
     * @return 妇产科考试-考场
     */
    @Override
    public List<GoScoreExamPlace> selectGoScoreExamPlaceList(GoScoreExamPlace goScoreExamPlace) {
        return baseMapper.selectGoScoreExamPlaceList(goScoreExamPlace);
    }

    @Override
    public List<GoScoreExamPlace> selectGoScoreExamPlaceByExamId(Long examId) {
        List<GoScoreExamPlace> goScoreExamPlaceList = baseMapper.selectGoScoreExamPlaceByExamId(examId);
        List<GoScoreExamStudent> goScoreExamStudents = goScoreExamStudentService.selectGoScoreExamStudentByExamId(examId);
        Map<Long, List<GoScoreExamStudent>> goScoreExamStudentsMap = goScoreExamStudents.stream().filter(o->
             ObjUtil.isNotNull(o) && ObjUtil.isNotNull(o.getExamPlaceId())
        ).collect(Collectors.groupingBy(GoScoreExamStudent::getExamPlaceId));
        for (GoScoreExamPlace goScoreExamPlace : goScoreExamPlaceList) {
            List<GoScoreExamStudent> goScoreExamStudentList = goScoreExamStudentsMap.get(goScoreExamPlace.getId());
            goScoreExamPlace.setGoScoreExamStudentList(goScoreExamStudentList);
        }
        return goScoreExamPlaceList;
    }

    /**
     * 新增妇产科考试-考场
     *
     * @param goScoreExamPlace 妇产科考试-考场
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoScoreExamPlace(GoScoreExamPlace goScoreExamPlace) {
//        int rows = baseMapper.insertGoScoreExamPlace(goScoreExamPlace);
//        insertGoScoreExamStudent(goScoreExamPlace);
//        return rows;
        return 0;
    }

    @Override
    @Transactional
    public int batchInsertGoScoreExamPlace(Long goScoreExamId, List<GoScoreExamPlace> goScoreExamPlaces) {
        if (ListUtil.isNotEmpty(goScoreExamPlaces)){
            goScoreExamPlaces = goScoreExamPlaces.stream().peek(o->o.setExamId(goScoreExamId)).collect(Collectors.toList());
        }
        int i = baseMapper.batchInsertGoScoreExamPlace(goScoreExamPlaces);
        for (GoScoreExamPlace goScoreExamPlace : goScoreExamPlaces) {
            goScoreExamStudentService.batchInsertGoScoreExamStudent(goScoreExamId,goScoreExamPlace.getId(),goScoreExamPlace.getGoScoreExamStudentList());
        }
        return i;
    }

    /**
     * 修改妇产科考试-考场
     *
     * @param goScoreExamPlace 妇产科考试-考场
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoScoreExamPlace(GoScoreExamPlace goScoreExamPlace) {
//        baseMapper.deleteGoScoreExamStudentByExamPlaceId(goScoreExamPlace.getId());
//        insertGoScoreExamStudent(goScoreExamPlace);
//        return baseMapper.updateGoScoreExamPlace(goScoreExamPlace);
        return 0;
    }

    /**
     * 批量删除妇产科考试-考场
     *
     * @param ids 需要删除的妇产科考试-考场主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoScoreExamPlaceByIds(Long[] ids) {
        goScoreExamStudentService.deleteGoScoreExamStudentByExamPlaceIds(ids);
        return baseMapper.deleteGoScoreExamPlaceByIds(ids);
    }

    /**
     * 删除妇产科考试-考场信息
     *
     * @param id 妇产科考试-考场主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoScoreExamPlaceById(Long id) {
        goScoreExamStudentService.deleteGoScoreExamStudentByExamPlaceId(id);
        return baseMapper.deleteGoScoreExamPlaceById(id);
    }

    @Override
    public int deleteGoScoreExamPlaceByExamId(Long examId) {
        goScoreExamStudentService.deleteGoScoreExamStudentByExamId(examId);
        return baseMapper.deleteGoScoreExamPlaceByExamId(examId);
    }

    @Override
    public int deleteGoScoreExamPlaceByExamIds(Long[] ids) {
        goScoreExamStudentService.deleteGoScoreExamStudentByExamIds(ids);
        return baseMapper.deleteGoScoreExamPlaceByExamIds(ids);
    }
}
